home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / smaltalk.lha / smalltalk-1.1.1 / Semaphore.st < prev    next >
Text File  |  1991-09-12  |  2KB  |  86 lines

  1. "======================================================================
  2. |
  3. |   Semaphore Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbyrne     19 Sep 89      Converted to use real method categories.
  34. |
  35. | sbyrne     25 Apr 89      created.
  36. |
  37. "
  38.  
  39. LinkedList subclass: #Semaphore
  40.        instanceVariableNames: 'signals'
  41.        classVariableNames: ''
  42.        poolDictionaries: ''
  43.        category: nil.
  44.  
  45. Semaphore comment: 
  46. 'My instances represent counting semaphores.  I provide methods for signalling
  47. the semaphore''s availability, and methods for waiting for its availability.
  48. I also provide some methods for implementing critical sections.  I currently
  49. do not (because the underlying system does not) support asynchronous
  50. signals, such as might be generated by C signals.' !
  51.  
  52. !Semaphore class methodsFor: 'instance creation'!
  53.  
  54. new
  55.     ^self basicNew initSemaphore
  56. !
  57.  
  58. forMutualExclusion
  59.     | sem |
  60.     sem _ self new.
  61.     sem signal.
  62.     ^sem
  63. !!
  64.  
  65.  
  66.  
  67. !Semaphore methodsFor: 'mutual exclusion'!
  68.  
  69. critical: aBlock
  70.     | result |
  71.     self wait.
  72.     result _ aBlock value.
  73.     self signal.
  74.     ^result
  75. !!
  76.  
  77.  
  78.  
  79. !Semaphore methodsFor: 'private'!
  80.  
  81. initSemaphore
  82.     signals _ 0
  83. !!
  84.